<%@ include file="/WEB-INF/views/commonJSTL.jsp" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>${s_UserPharmacy.pharmacy.name}</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<%@ include file="/WEB-INF/views/commonResources.jsp" %>

<script src="<c:url value="/resources/js/jquery.print.js" />"></script>

<script type="text/javascript">
	$(document).ready(function(){
		$.fn.imprimir = function(){
            $( "#mainShowTable" ).print();
            return( false );
        }		
		$("#formBookSale").validate({
	        submitHandler: function(form) {
	        	pressButton();
	        }
	    });		
	});
	
	function showTable(data,html){		
		html+='	<div>'+
		'			<table style="width: 100%;text-align: center" class="tabla_decorado">'+  
		'				<thead>'+
		'					<tr>'+
		'						<th colspan="3" rowspan="2" class="th_decoradoRowspan" style="text-align: center;width: 12%;"><fmt:message key="table.date"/></th>'+
		'						<th rowspan="3" class="th_decoradoRowspan" style="text-align: center;width: 9%;"><fmt:message key="table.numNiCiClient"/></th>'+		
		'						<th rowspan="3" class="th_decoradoRowspan" style="text-align: center;width: 16%;"><fmt:message key="table.nameSocialReason"/></th>'+
		'						<th rowspan="3" class="th_decoradoRowspan" style="text-align: center;width: 9%;"><fmt:message key="table.numBill"/></th>'+				
		'						<th rowspan="3" class="th_decoradoRowspan" style="text-align: center;width: 9%;"><fmt:message key="table.numAuthorization"/></th>'+		
		'						<th rowspan="3" class="th_decoradoRowspan" style="text-align: center;width: 9%;"><fmt:message key="table.codeController"/></th>'+		
		'						<th colspan="5" class="th_decoradoRowspan" style="text-align: center;"><fmt:message key="table.figureInBolivian"/></th>'+			
		'					</tr>'+
		'					<tr>'+
		'						<th class="th_decoradoRowspan" style="text-align: center;width: 6%;"><fmt:message key="table.total"/> <fmt:message key="table.bill"/></th>'+
		'						<th class="th_decoradoRowspan" style="text-align: center;width: 6%;"><fmt:message key="table.total"/> I.C.E</th>'+
		'						<th class="th_decoradoRowspan" style="text-align: center;width: 6%;"><fmt:message key="table.exemptAmount"/></th>'+		
		'						<th class="th_decoradoRowspan" style="text-align: center;width: 6%;"><fmt:message key="table.amountNeto"/></th>'+		
		'						<th rowspan="2" class="th_decoradoRowspan" style="text-align: center;width: 6%;"><fmt:message key="table.taxDebit"/> IVA</th>'+            
		'					</tr>'+
		'					<tr>'+
		'						<th class="th_decoradoRowspan" style="text-align: center;width: 4%;"><fmt:message key="table.day"/></th>'+			
		'						<th class="th_decoradoRowspan" style="text-align: center;width: 4%;"><fmt:message key="table.month"/></th>'+
		'						<th class="th_decoradoRowspan" style="text-align: center;width: 4%;"><fmt:message key="table.year"/></th>'+
		'						<th class="th_decoradoRowspan" style="text-align: center;width: 7%;">(A)</th>'+
		'						<th class="th_decoradoRowspan" style="text-align: center;width: 7%;">(B)</th>'+
		'						<th class="th_decoradoRowspan" style="text-align: center;width: 7%;">(C)</th>'+
		'						<th class="th_decoradoRowspan" style="text-align: center;width: 7%;">(A.B.C)</th>'+
		'					</tr>'+
		'				</thead>'+
		'				<tbody>';
		
		var total	 		= 0;
		var totalICE 		= 0;
		var totalExcentos	= 0;
		var totalNeto		= 0;
		var totalGeneral	= 0;
		
		for(var i=0; i<data.length; i++){
			var obj = data[i];			
			if(obj != null){
				var d = new Date(obj.bill.date);
				var ice		 = 0.00;
				var excentos = 0.00;
				var neto = obj.bill.amount-excentos;
				var totalGeneralTemp = ((obj.bill.amount)*(obj.bill.dosageId.iva)/100);
				html+='		<tr>'+
				'				<td class="th_decorado" style="text-align: center"> '+addZeroIfMinorToOther(d.getDate(),10)+' </td>'+
				'				<td class="th_decorado" style="text-align: center"> '+addZeroIfMinorToOther((d.getMonth()+1),10)+' </td>'+
				'				<td class="th_decorado" style="text-align: center"> '+(d.getFullYear())+' </td>'+
				'				<td class="th_decorado" style="text-align: center"> '+obj.bill.nit+' </td>'+
				'				<td class="th_decorado" style="text-align: center"> '+obj.clientId.name+' </td>'+
				'				<td class="th_decorado" style="text-align: center"> '+obj.bill.billNum+' </td>'+
				'				<td class="th_decorado" style="text-align: center"> '+obj.bill.dosageId.orderNumber+' </td>'+
				'				<td class="th_decorado" style="text-align: center"> '+obj.bill.controlCode+' </td>'+
				'				<td class="th_decorado" style="text-align: center"> '+(obj.bill.amount).toFixed(2)+' </td>'+
				'				<td class="th_decorado" style="text-align: center"> '+roundNumber(ice,2).toFixed(2)+' </td>'+
				'				<td class="th_decorado" style="text-align: center"> '+roundNumber(excentos,2).toFixed(2)+' </td>'+
				'				<td class="th_decorado" style="text-align: center"> '+roundNumber(neto,2).toFixed(2)+' </td>'+
				'				<td class="th_decorado" style="text-align: center"> '+roundNumber(totalGeneralTemp,2).toFixed(2)+' </td>'+
				'			</tr>';
				total += roundNumber(obj.bill.amount,2);
				totalICE += roundNumber(ice,2);
				totalExcentos += roundNumber(excentos,2);
				totalNeto += roundNumber(neto,2);
				totalGeneral += roundNumber(totalGeneralTemp,2);
			}
		}
		if(data.length == 0)
			html += '<tr class="tr_decorado"><td class="td_decorado" colspan="13" style="text-align:center"><span style="color:red"><fmt:message key="show.noExistTransaction"/></span></td></tr>';
		else{
			html+='		<tr>'+
			'				<td class="th_decorado" style="text-align: center" colspan="7"> &nbsp; </td>'+
			'				<td class="th_decorado" style="text-align: center"> PARCIALES </td>'+
			'				<td class="th_decorado" style="text-align: center" colspan="5"> &nbsp; </td>'+
			'			</tr>';	
			html+='		<tr>'+
			'				<td class="th_decorado" style="text-align: center"> &nbsp; </td>'+
			'				<td class="th_decorado" style="text-align: center"> &nbsp; </td>'+
			'				<td class="th_decorado" style="text-align: center"> &nbsp; </td>'+
			'				<td class="th_decorado" style="text-align: center"> &nbsp; </td>'+
			'				<td class="th_decorado" style="text-align: center"> &nbsp; </td>'+
			'				<td class="th_decorado" style="text-align: center"> &nbsp; </td>'+
			'				<td class="th_decorado" style="text-align: center"> &nbsp; </td>'+
			'				<td class="th_decorado" style="text-align: center"> TOTALES </td>'+
			'				<td class="th_decorado" style="text-align: center"> '+roundNumber(total,2).toFixed(2)+' </td>'+
			'				<td class="th_decorado" style="text-align: center"> '+roundNumber(totalICE,2).toFixed(2)+' </td>'+
			'				<td class="th_decorado" style="text-align: center"> '+roundNumber(totalExcentos,2).toFixed(2)+' </td>'+
			'				<td class="th_decorado" style="text-align: center"> '+roundNumber(totalNeto,2).toFixed(2)+' </td>'+
			'				<td class="th_decorado" style="text-align: center"> '+roundNumber(totalGeneral,2).toFixed(2)+' </td>'+
			'			</tr>';	
		}			
		html +='		</tbody>'+
		'			</table>'+
		'		</div>';	
		return html;
	}	
	
	function pressButton(){
		var html='';
		loadTable(html);
	}
	function loadTable(html){
		$.ajax({ 
    		type: "GET",
    		url	: 'bookSalesByGestionByPeriod',
       	   	dataType: "json",		
            data: {month:$("#month").val(),year:$("#year").val(),radioSelected:$('input:radio[name=typeDate_0]:checked').val()},
       	    success:function(data){	 
       	    	var showHtml=showTable(data,showTableHead());
       	    	$("#mainShowTable").html(showHtml);
       		}, 
       		error: 	function(){alert('Error in request..from bookSalesByGestionByPeriod');} }); 
	}
	
	function showTableHead(){
		var html='';
		html+=''+
		'	<table border="0" style="width: 100%">'+
		'		<thead>'+
		'			<tr>'+
		'				<th style="width: 60%">&nbsp;</th>'+
		'				<th style="width: 40%;text-align:right; border: 0px; margin:0px 0px 0px; padding:0px 4px 0px;">'+
		'					<a title="Imprimir" id="printImg" onClick="$.fn.imprimir()" style="cursor: pointer;">'+
		'						<img src="<c:url value="/resources/images/impresora.gif"/>" alt="imprimir.gif" title="Imprimir" style="border: 0px;" />'+
		'					</a>'+
		'					<a title="Imprimir" id="printLabel" onClick="$.fn.imprimir()" style="cursor: pointer;">Imprimir</a>'+
		'				</th>'+
		'			</tr>'+
		'		</thead>'+
		'	</table>'+
		'	<table style="width: 100%">'+
		'		<tr>'+
		'			<td colspan="2" style="width: 100%">'+
		'				<table style="width: 100%">'+
		'					<tr>'+
		'						<td width="88%" style="text-align: left;" > &nbsp; </td>'+
		'						<td width="6%" style="text-align: center;border: 1px solid #000;font-size: 8pt;" >FOLIO</td>'+
		'						<td width="6%" style="text-align: center;border: 1px solid #000;font-size: 8pt;"> 1 </td>'+
		'					</tr>'+
		'				</table>'+
		'			</td>'+
		'		</tr>'+
		'	</table>'+
		'	<table border="0" style="width: 100%" >'+
		'		<tr>'+
		'			<td style="width: 60%">'+
		'				<table>'+
		'					<tr>'+
		'						<td style="width: 18%;font-size: 8pt;"><b>LIBRO DE VENTAS IVA DEL PERIODO</b></td>'+
		'						<td style="width: 4%;border: 1px solid #000;text-align: center;font-size: 8pt;">month</td>'+
		'						<td style="width: 6%;border: 1px solid #000;text-align: center;font-size: 8pt;">year</td>'+
		'						<td style="width: 11%">&nbsp;</td>'+
		'					</tr>'+
		'				</table>'+
		'			</td>'+
		'			<td style="width: 40%">&nbsp;</td>'+
		'		</tr>'+
		'	</table>'+
		'	<table border="0" style="border: 1px solid #000000;width: 100%" cellspacing="7">'+
		'		<tr>'+
		'			<td style="text-align: right;width: 20%;font-size: 8pt;">NOMBRE O RAZ&Oacute;N SOCIAL : </td>'+
		'			<td style="border: 1px solid #000000;text-align: left;width: 30%;font-size: 8pt;"> razon social </td>'+
		'			<td style="text-align: right; width: 10%;font-size: 8pt;" >NIT : </td>'+
		'			<td style="border: 1px solid #000000;text-align: center;width: 40%;font-size: 8pt;">nit number</td>'+
		'		</tr>'+
		'		<tr>'+
		'			<td style="text-align: right;font-size: 8pt;">No SUCURSAL : </td>'+
		'			<td style="border: 1px solid #000000;text-align: left;font-size: 8pt;"> 1 </td>'+
		'			<td style="text-align: right;font-size: 8pt;">DIRECCI&Oacute;N : </td>'+
		'			<td style="border: 1px solid #000000;text-align: left;font-size: 8pt;">direccion</td>'+
		'		</tr>'+
		'	</table><br/>';
		return html;		
	}
	
	
	
	/*
	function pressButton(){
		var text = ($('input:radio[name=typeDate_0]:checked').val() == 1)?($("#fecha_0").val()):('<fmt:message key="form.from"/>:'+$("#dateFrom_0").val()+' <fmt:message key="form.to"/> '+$("#dateTo_0").val());
		html =	'	<h2 class="decorado"><fmt:message key="title.transactions"/> '+text+'</h2></br>';
		if($('input:radio[name=typeDate_0]:checked').val() == 1){
			var date 	= $("#fecha_0").val();
			loadListByDateStartByDateEnd(date,date,$("#userId").val(),html);
		}else{
			var dateStart 	= $("#dateFrom_0").val();
			var dateEnd 	= $("#dateTo_0").val();
			loadListByDateStartByDateEnd(dateStart,dateEnd,$("#userId").val(),html);
		}
	}
	*/
	
	
	
	
</script>
</head>

<body>
<div id="top-wrap">
	<%@include file="../../../head.jsp"  %>
	<div id="bodySpace">
		
	    <div class="clearfix" id="middle" style="position: absolute">  <!-- aqui va la ruta -->
	        <!-- aqui va el arbol de entrada -->
	        <div id="breadcrumb">
	            <ul>
                    <li><a href="<spring:url value="/mainFront?" htmlEscape="true" />">Home</a></li>	
                    <li><span style="color:#666"><fmt:message key="title.operation"/></span></li>		
                    <li><span style="color:#666"><fmt:message key="title.nationalTaxation"/></span></li>		
                    <li class="current-page"><span class="end"><span class="middle"><fmt:message key="title.bookSales"/></span></span></li>
                </ul>
	        </div>
	    </div>
	    
	    <div id="principal">
            <!-- izquierda -->
            <div class="cuerpo2y" style="border: 0px solid"><br/><br/><!-- estos break son necesarios ya q uso absolute en css  cuerpo izquierdo-->
                <div class="izq">
					<%@include file="../../../bodyIzq.jsp" %>	
                </div>
                
                <div class="medio" style="border: 0px solid">
					<h2 class="decorado"><fmt:message key="title.bookSales"/></h2><br></br> 
										
					<div id="imgLoad" style="float: left;width: 100%;display: none;"><img src="<c:url value="/resources/images/imgLoad/ajax-loader.gif" />" width="32" height="32" alt="ajax-loader.gif" style="position:absolute; top:30%; left:50%;"/></div> 
					<div id="bookSales">
						<form id="formBookSale">
							<div style="clear: both">
								<label class="form_align35"><fmt:message key="form.taxpayer"/> :</label>
								<label class="form_labelAlign"><b>${s_UserPharmacy.pharmacy.socialReason}</b></label>
							</div>
							<div style="clear: both">
								<label class="form_align35"><fmt:message key="form.taxpayer"/> :</label>
								<label class="form_labelAlign"><b>NACIONAL</b></label>
							</div>
							<div style="clear: both">															
								<label class="form_align35"><fmt:message key="form.typeSale"/> :</label>						
								<label class="form_labelAlign">
									<input type="radio" name="sales" value="1" id="sales_1" checked="checked" title="Pulsa aqu&iacute; para ventas del sistema"/><label for="sales_1"> Ventas del Sistema</label>
									<input type="radio" name="sales" value="2" id="sales_2" title="Pulsa aqu&iacute; para ventas manuales"/><label for="sales_2"> Ventas Manuales</label>
								</label>
							</div>
							<div style="clear: both">
								<label class="form_align35"><fmt:message key="form.gestion"/> :</label>				
								<select name="year" id="year" class="form_input_align required" title="<fmt:message key="show.form.selected.gestion"/>">
									<option value=""><fmt:message key="show.form.selected"/></option>						
									<c:forEach items="${listYear}" var="year">
									    <option value="${year}">${year}</option>
									</c:forEach>
								</select>
							</div>
							<div style="clear: both">
								<label class="form_align35"><fmt:message key="form.period"/> :</label>				
								<select name="month" id="month" class="form_input_align required" title="<fmt:message key="show.form.selected.period"/>">
									<option value=""><fmt:message key="show.form.selected"/></option>
									<c:set var="i" value="0"/>
									<c:forEach items="${listMonths}" var="month">
									    <option value="${i}">${month}</option>
									    <c:set var="i" value="${i+1}"/>
									</c:forEach>
								</select>
							</div>
							<div style="clear: both">
								<label class="form_align35"><fmt:message key="form.format"/> :</label>			
								<label class="form_labelAlign">
									<input type="radio" name="format" value="1" id="format_1" checked="checked" title="Pulsa aqu&iacute; para subir al sistema"/><label for="format_1"> Para subir al Sistema&nbsp;</label>
									<input type="radio" name="format" value="2" id="format_2" title="Pulsa aqu&iacute; para imprimir libro de ventas"/><label for="format_2"> Para imprimir libro de ventas</label>
								</label>
							</div>
							<div style="clear: both"><br/>
								<label class="form_align35">&nbsp;</label>
								<input type="submit" class="boton" value="Ver Libro de Ventas"/> &nbsp;&nbsp;&nbsp;
								<input type="reset" class="boton" value="<fmt:message key="button.clean"/>" />
							</div>
						</form><br/>
						
						<div id="mainShowTable" style="height:390px;overflow:auto;"></div>
						
					</div>	  
                </div>
            </div><!--end of leftPan-->
            
            <div class="yDer" style="border: 1px solid #ccc;">
                  <%@include file="../../../notices.jsp" %>                       
            </div><!--end of rightPan-->
        </div><!--end main content -->
	</div>
</div>
</body>
</html>
